package com.carepad.action.input;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.carepad.action.AbstractManagementAction;
import com.carepad.entity.RecordItems;
import com.carepad.entity.mapper.EnJaDate;
import com.carepad.exception.ItemNotFoundException;
import com.carepad.exception.NullReferenceAccessException;
import com.carepad.service.RecordItemsGroupService;
import com.carepad.service.RecordItemsInputService;
import com.carepad.util.DateConverterEnJa;

public abstract class AbstractRecordItemsInputAction extends AbstractManagementAction {

	private static final long serialVersionUID = 1L;

	@Qualifier("recordItemsInputService")
	@Autowired protected RecordItemsInputService recordItemsInputService;
	
	@Qualifier("recordItemsGroupService")
	@Autowired protected RecordItemsGroupService recordItemsGroupService;
	
	protected RecordItems recordItems;

	public RecordItems findById() {
		if(uid != null) {
			try{
				return recordItemsInputService.findById(uid);
			}catch(Exception e) {
				throw new ItemNotFoundException("Id : " + uid);
			}
		} else {
			//logger
			throw new NullReferenceAccessException("Id cannot be null");
		}
	}
	
	public void setSessionExecutionDate(String executionDate) {
		
		EnJaDate ejDate = getSessionImplementationDate();
		
		if(executionDate != null){
			try {
				String stringEnDate = DateConverterEnJa.JaDatetoEnString(executionDate);		
				SimpleDateFormat fmtr = new SimpleDateFormat("yyyy-MM-dd");
				Date endate = fmtr.parse(stringEnDate);
				
				ejDate.setEnDate(endate);						
				
			} catch (ParseException e) {				
				e.printStackTrace();
			}
		}		
		else{
			ejDate.setEnDate(new Date());
		}
		
		addSessionVariable(ejDate);	
	}

	public RecordItems getRecordItems() {
		return recordItems;
	}

	public void setRecordItems(RecordItems recordItems) {
		this.recordItems = recordItems;
	}	
}
